package com.linran.webjsonrsp.websocket.handler;

import com.linran.webjsonrsp.websocket.pricipal.StompPrincipal;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.security.Principal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Map;

/**
 * Principal主体手动生成Handler
 *
 * 配置为为每个连接生成唯一的主体名称.它将作为StomHeaderAccessor对象的一部分传递该主体,您可以通过连接事件侦听器,MessageMapping函数等访问它们
 *
 * @author lin
 * @date 2020-08-28 14:59
 */
public class StompPrincipalHandshakeHandler extends DefaultHandshakeHandler {

    @Override
    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
        System.out.println("生成Principal");
        //可以通过request解析用户信息，生成用户Principal对象实体
        StompPrincipal stompPrincipal = new StompPrincipal();
        stompPrincipal.setName("User"+ LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        return stompPrincipal;
    }
}
